package tech.yunland.shop.web;

import com.moyuer.bsf.common.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tech.yunland.shop.domain.model.Exam;
import tech.yunland.shop.domain.model.Score;
import tech.yunland.shop.domain.service.ScoreService;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by v on 17-7-3.
 */
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(@RequestParam(required = false) String sid,
                     @RequestParam(required = false) BigDecimal score) {
        scoreService.save(sid, score);
    }

    /**
     * 查询参加该场考试全部学生的成绩
     *
     * @param eid
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public Pagination<Score> get(@RequestParam(required = false) String eid,
                                 @RequestParam(required = false) Integer page,
                                 @RequestParam(required = false) Integer rows) {
        Pagination result = new Pagination(page, rows);
        Integer startIdx = result.getStartIdx();
        Integer count = scoreService.getSize(eid);
        List<Score> records = scoreService.get(eid, startIdx, rows);
        result.setCount(count);
        result.setRecords(records);
        return result;
    }

    @RequestMapping(value = "/getScoreByDate", method = RequestMethod.GET)
    public Pagination<Score> getScoreByDate(@RequestParam(required = false) String desid,
                                            @RequestParam(required = false) String uid,
                                            @RequestParam(required = false) Integer page,
                                            @RequestParam(required = false) Integer rows) {
        Pagination result = new Pagination(page, rows);
        Integer startIdx = result.getStartIdx();
        Integer count = scoreService.getScoreByDateSize(desid, uid);
        List<Score> records = scoreService.getScoreByDate(desid, uid, startIdx, rows);
        result.setCount(count);
        result.setRecords(records);
        return result;
    }
}
